package com.ky.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.user.entity.MoneyLog;
import com.ky.user.entity.PayAdd;
import com.ky.user.entity.PayLog;
import com.ky.user.mapper.MoneyLogMapper;
import com.ky.user.mapper.PayLogMapper;
import com.ky.user.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/user/wallet")
public class WalletController {

    @Autowired
    private WalletService walletService;
    @Autowired
    private MoneyLogMapper moneyLogMapper;


    @Autowired
    private PayLogMapper payLogMapper;
    @RequestMapping("/my")
    public R findAllWallet(HttpServletRequest request){

        String token = request.getHeader(SystemConfig.HEADER_TOKEN);

        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();

        return walletService.findAllWallet(id);
    }

    @RequestMapping("/create")
    public R create(HttpServletRequest request, @RequestBody PayAdd payAdd){

        // 1 判断请求参数
        if(payAdd==null){

            return R.fail("参数错误");
        }


        // 配置userid
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);

        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        payAdd.setUserId(id);

        // 2更具请求参数 支付方式不同 调用不同的支付

        switch (payAdd.getPayType()){

            case 2:// 支付宝支付
                return  walletService.rechargeWalletByZhiFuBao(payAdd) ;
            case 3:// 微信支付

//                break;
//            return   ;

            default:

                return R.fail("支付类型错误");
        }


    }
//    @RequestMapping("/findAllWallet")
//    public R findAllWallet(HttpServletRequest request){
//
//        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
//
//        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
//
//        return walletService.findAllWallet(id);
//    }
    @RequestMapping("/findAllPayLog")
    public R findAllPayLog(HttpServletRequest request){

        String token = request.getHeader(SystemConfig.HEADER_TOKEN);

        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();

        return walletService.findAllPayLog(id);
    }
    @RequestMapping("/findAllByType")
    public R findAllPayLogByType(@RequestParam("type")Integer type, HttpServletRequest request){

        String token = request.getHeader(SystemConfig.HEADER_TOKEN);

        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        if (type==2){
            //根据用户id和充值记录查询
            new QueryWrapper<PayLog>().eq("user_id",id);
            return R.ok(payLogMapper.selectList(new QueryWrapper<PayLog>().eq("user_id",id)));
        }else {
        //根据用户id和消费记录查询
         new QueryWrapper<MoneyLog>().eq("user_id",id);

        return R.ok(moneyLogMapper.selectList(new QueryWrapper<MoneyLog>().eq("user_id",id)));
    }
    }

}

